package terra.model.object.knownlist;

import java.util.concurrent.ConcurrentHashMap;

import terra.model.object.GameObject;
import terra.model.object.Player;
import terra.network.packet.server.ObjectDelete;

public class PlayerKnownList extends ObjectKnownList {

	private static final long serialVersionUID = -509062914279065533L;
	private ConcurrentHashMap<Integer, Player> players = new ConcurrentHashMap<Integer, Player>();

	public PlayerKnownList(Player obj) {
		super(obj);
		knowDistance = obj.getHandler().getClientWidth()
				+ obj.getHandler().getClientHeight() / 2;
	}

	@Override
	public boolean addKnownObject(GameObject object) {
		if (super.addKnownObject(object)) {
			if (object instanceof Player) {
				((Player) object).send(getOwner().getInfo());
				getPlayers().put(object.getObjectId(), (Player) object);
			} else {
				getOwner().send(object.getInfo());
			}
//			else if (object instanceof NpcObject) {
//				getOwner().send(((NpcObject) object).getInfo());
//			} else if (object instanceof Tree) {
//				getOwner().send(((Tree) object).getInfo());
//			} else if (object instanceof StaticObject) {
//				getOwner().send(((StaticObject) object).getInfo());
//			}
			return true;
		}
		return false;
	}

	@Override
	public boolean removeKnownObject(GameObject object) {
		if (super.removeKnownObject(object)) {
			if (object instanceof Player) {
				getPlayers().remove(object.getObjectId());
			}
			getOwner().send(new ObjectDelete(object.getObjectId()));
			return true;
		}
		return false;
	}

	public Player getOwner() {
		return (Player) super.getOwner();
	}

	public void removeAllKnownObjects() {
		super.removeAllKnownObjects();
		getPlayers().clear();
	}

	public ConcurrentHashMap<Integer, Player> getPlayers() {
		return players;
	}

}
